package com.rfid.emergencywarehouse.presenter;

import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.ReturnRes;
import com.rfid.emergencywarehouse.net.entity.WGStatusRes;
import com.rfid.emergencywarehouse.view.IReturnView;

import java.util.ArrayList;
import java.util.List;

public class ReturnAssetPresenter extends ReadTagPresenter<IReturnView> {


    public ReturnAssetPresenter() {
        setDisableShelf(true);
    }
    public void getWGStatusConfigs() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    WGStatusRes res = NetApi.INSTANCE.getWGStatusConfig();
                    String value = res.getData().getValue();
                    String values[] = value.split(",");
                    List<NameValueBean> beans = new ArrayList<>();
                    for (String v : values) {
                        NameValueBean b = new NameValueBean();
                        b.setName(v);
                        b.setValue(v);
                        beans.add(b);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onGetWGStatusConfigs(beans);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void returnAsset(List<BarcodeInfo> barcodes) {
        mView.onShowLoading(null);
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean s = false;
                String msg = "";
                try {
                    ReturnRes res = NetApi.INSTANCE.returnAsset(barcodes);
                    if (res.getCode() == 0) {
                        s = true;
                    } else {
                        msg = res.getMsg();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    s = false;
                    msg = e.getMessage();
                }
                String finalMsg = msg;
                boolean finalS = s;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mView != null) {
                            mView.onHideLoading();
                            mView.onReturnResult(finalS, finalMsg);
                        }
                    }
                });
            }
        });
    }
}
